package com.apache.uct.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SystemTools;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.uct.common.entity.RoleGive;
import com.apache.uct.manager.RoleGiveManager;
import com.apache.uct.service.plugins.ActCacheHelper;

public class RoleGiveManagerImpl implements RoleGiveManager {

	protected IDao roleGiveDao;
	protected final String entityName = "com.apache.uct.common.entity.RoleGive";

	public void setRoleGiveDao(IDao roleGiveDao) {
		this.roleGiveDao = roleGiveDao;
	}

	public String saveInfo(ParamsVo<RoleGive> vo) throws BusinessException {
		RoleGive roleGive = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="roleGive_"+infoId;
		roleGive.setGiveId(infoId);
		MethodParam param = new MethodParam("RoleGive", cacheKey, "", entityName);
		param.setVaule(roleGive);
		if (roleGiveDao.insert(param)) {
			ActCacheHelper.getInstance().writeRoleUser(roleGive.getUserId(), roleGive.getRoleId(), true);
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<RoleGive> vo) throws BusinessException {
		RoleGive roleGive = vo.getObj();
		if (Validator.isNotNull(roleGive.getGiveId())) {
			String cacheKey = "";
			//String cacheKey="roleGive_"+roleGive.getGiveId();
			MethodParam param = new MethodParam("RoleGive", cacheKey, "", entityName);
			param.setVaule(roleGive);
			return roleGiveDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<RoleGive> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		RoleGive roleGive = vo.getObj();
		if (Validator.isNotNull(infoId)) {
			String cacheKey = "";
			//String cacheKey="roleGive_"+infoId;
			String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
			MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
			param.setInfoId(infoId);
			RoleGive info = (RoleGive) roleGiveDao.selectById(param);
			if (Validator.isEmpty(info)) {
				return false;
			}
			param.setVaule(info);//此处需要先将状态值赋值为删除状态
			if ("false".equals(mark)) {//逻辑删除
				param.setKey("RoleGive");
				return roleGiveDao.edit(param);
			} else {
				param.setParams("giveId", infoId);
				param.setDelete(true);
				return roleGiveDao.delete(param);
			}
		} else if (!Validator.isEmpty(roleGive)) {
			MethodParam param = new MethodParam("ByRoleIdOrUserId", "", "", entityName);
			String hql = "";
			if (Validator.isNotNull(roleGive.getRoleId()) && Validator.isNotNull(roleGive.getUserId())) {
				hql = roleGiveDao.getSql(3) + " roleId=:roleId and userId=:userId";
				param.setParams("roleId", roleGive.getRoleId());
				param.setParams("userId", roleGive.getUserId());
			} else if (Validator.isNotNull(roleGive.getRoleId())) {
				hql = roleGiveDao.getSql(3) + " roleId=:roleId";
				param.setParams("roleId", roleGive.getRoleId());
			} else if (Validator.isNotNull(roleGive.getUserId())) {
				hql = roleGiveDao.getSql(3) + " userId=:userId";
				param.setParams("userId", roleGive.getUserId());
			}
			param.setDelete(true);
			param.setSqlStr(hql);
			boolean delFlag = roleGiveDao.delete(param);
			if (delFlag) {
				if (Validator.isNotNull(roleGive.getUserId()) && Validator.isNotNull(roleGive.getRoleId())) {
					ActCacheHelper.getInstance().writeRoleUser(roleGive.getUserId(), roleGive.getRoleId(), false);
				} else if (Validator.isNotNull(roleGive.getUserId()) && Validator.isNull(roleGive.getRoleId())) {
					ActCacheHelper.getInstance().delRoleUserByUserId(roleGive.getUserId());
				}
			}
			return delFlag;
		}
		return false;
	}

	public Object getInfoById(ParamsVo<RoleGive> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="roleGive_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return roleGiveDao.selectById(param);
	}

	public Object execute(ParamsVo<RoleGive> vo) {
		String key = vo.getKey();
		if ("userToRole".equals(key)) {
			return userToRolePage(vo);
		}
		return null;
	}

	private Page userToRolePage(ParamsVo<RoleGive> vo) {
		String userId = String.valueOf(vo.getParams("userId"));
		String hql = roleGiveDao.getSql(4);
		MethodParam param = new MethodParam("ByUserToRole", "", "", entityName);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		param.setParams("userId", userId);
		if ("1".equals(SystemTools.getInstance().getValue("org_act_switch"))) {//是否开启授权
			String userOrgId = String.valueOf(vo.getParams("userOrgId"));
			String sysUser = String.valueOf(vo.getParams("sysUser"));

			if (Validator.isNotNull(userOrgId)) {
				hql += " and r.roleId in(select roleId from UCT_ROLE_ORG where orgId=:orgId )";
				param.setParams("orgId", userOrgId);
			} else {
				hql += " and r.roleId in('')";
			}
			if ("1".equals(sysUser)) {
				param.setKey("ByUserToRoleForSys");
			} else {
				if ("0".equals(sysUser)) {
					param.setParams("sysUser", " and g.user_id ='" + userId + "'");
				} else {
					param.setParams("sysUser", " or r.del_status is null");
				}
			}
		}
		param.setSqlStr(hql + " ORDER BY r.roleCname asc");
		Page page = roleGiveDao.pageSelect(param);
		return page;
	}

	public Page getPageInfo(ParamsVo<RoleGive> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = roleGiveDao.pageSelect(param);
		return page;
	}

	public List<RoleGive> getList(ParamsVo<RoleGive> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return roleGiveDao.select(param);
	}

	public long countInfo(ParamsVo<RoleGive> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return roleGiveDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<RoleGive> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(roleGiveDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		RoleGive roleGive = vo.getObj();
		if (Validator.isNotNull(roleGive.getSysEname())) {
			sb.append(" and sysEname =:sysEname");
			param.setParams("sysEname", roleGive.getSysEname());
		}
		if (Validator.isNotNull(roleGive.getUserId())) {
			sb.append(" and userId =:userId");
			param.setParams("userId", roleGive.getUserId());
		}
		if (Validator.isNotNull(roleGive.getUserEname())) {
			sb.append(" and userEname =:userEname");
			param.setParams("userEname", roleGive.getUserEname());
		}
		if (Validator.isNotNull(roleGive.getRoleId())) {
			sb.append(" and roleId =:roleId");
			param.setParams("roleId", roleGive.getRoleId());
		}
		if (Validator.isNotNull(roleGive.getRoleEname())) {
			sb.append(" and roleEname =:roleEname");
			param.setParams("roleEname", roleGive.getRoleEname());
		}
		if (Validator.isNotNull(roleGive.getTmpFlag())) {
			sb.append(" and tmpFlag =:tmpFlag");
			param.setParams("tmpFlag", roleGive.getTmpFlag());
		}
		if (Validator.isNotNull(roleGive.getGiveRemark())) {
			sb.append(" and giveRemark =:giveRemark");
			param.setParams("giveRemark", roleGive.getGiveRemark());
		}
		if (Validator.isNotNull(roleGive.getCreateUser())) {
			sb.append(" and createUser =:createUser");
			param.setParams("createUser", roleGive.getCreateUser());
		}
		if (Validator.isNotNull(roleGive.getUpdateUser())) {
			sb.append(" and updateUser =:updateUser");
			param.setParams("updateUser", roleGive.getUpdateUser());
		}

		param.setSqlStr(sb.toString());
		return param;
	}
}
